package com.zznj.xzjc.adapter.index;

import android.text.Html;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zznj.xzjc.R;
import com.zznj.xzjc.net.entity.IndexNewXianSuoBean;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.utils.TimeUtils;

import java.util.List;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/7
 */
public class ZhiShiKuAdapter extends BaseQuickAdapter<IndexNewXianSuoBean, BaseViewHolder> {

    public ZhiShiKuAdapter(int layoutResId, @Nullable List<IndexNewXianSuoBean> data) {
        super(layoutResId, data);
    }

    private String key;
    public void setKey(String key){
        this.key = key;
        notifyDataSetChanged();
    }

    /**
     * 数据类型（案例库：1，法律法规：2，罪名库：3，科技文献库：4，专家库：5，鉴定机构库：6，企业库：7，通知公告：15，工作动态：9，产品召回：12，政策发布：11，案例播报：10，权力清单：14，执法主体：13）
     * @param helper
     * @param item
     */
    @Override
    protected void convert(BaseViewHolder helper, IndexNewXianSuoBean item) {
        TextView labelLeft = helper.getView(R.id.labelLeft);
        TextView labelRight1 = helper.getView(R.id.labelRight1);
        TextView labelRight2 = helper.getView(R.id.labelRight2);
        TextView labelRight3 = helper.getView(R.id.labelRight3);

        TextView tv_title = helper.getView(R.id.tv_title);
        TextView tv_content1 = helper.getView(R.id.tv_content1);
        TextView tv_content2 = helper.getView(R.id.tv_content2);
        TextView tv_content3 = helper.getView(R.id.tv_content3);
        TextView tv_content4 = helper.getView(R.id.tv_content4);
        TextView tv_content5 = helper.getView(R.id.tv_content5);

        View linearView0 = helper.getView(R.id.linearView0);
        View linearView1 = helper.getView(R.id.linearView1);

        linearView0.setVisibility(View.GONE);
        linearView1.setVisibility(View.GONE);
        tv_content1.setVisibility(View.GONE);
        tv_content2.setVisibility(View.GONE);
        tv_content3.setVisibility(View.GONE);
        tv_content4.setVisibility(View.GONE);
        tv_content5.setVisibility(View.GONE);
        labelLeft.setVisibility(View.GONE);
        labelRight1.setVisibility(View.GONE);
        labelRight2.setVisibility(View.GONE);
        labelRight3.setVisibility(View.GONE);


        if(!StringUtils.isEmpty(key)){
            tv_title.setText(Html.fromHtml(StringUtils.delHtmlTags(item.getIndexTitle_s()).replace(key,"<span style='color:red;'>"+key+"</span>")));
        }else{
            tv_title.setText(Html.fromHtml(item.getIndexTitle_s()));
        }
        if(item.getDataType_i().equals("1")){
            //案例库
            labelLeft.setText(item.getWritType_s());
            labelRight1.setText(item.getCaseType_s());
            labelRight2.setText(item.getKeyWord_s());


            tv_content1.setText("地区："+StringUtils.toStringForWu(item.getDictionName_s()));
            tv_content2.setText("时间："+ TimeUtils.getTime(item.getJudgeDate_date(),"yyyy-MM-dd"));
            tv_content5.setText("领域："+StringUtils.toStringForWu(item.getSubjectLeader_s()));
            tv_content5.setVisibility(View.GONE);

            linearView0.setVisibility(View.VISIBLE);
            labelLeft.setVisibility(StringUtils.isEmpty(item.getWritType_s())?View.GONE:View.VISIBLE);
            labelRight1.setVisibility(StringUtils.isEmpty(item.getCaseType_s())?View.GONE:View.VISIBLE);
            labelRight2.setVisibility(StringUtils.isEmpty(item.getKeyWord_s())?View.GONE:View.VISIBLE);

            linearView1.setVisibility(View.VISIBLE);
            tv_content1.setVisibility(View.VISIBLE);
            tv_content2.setVisibility(View.VISIBLE);

        }else if(item.getDataType_i().equals("2")){
            //法律法规
            labelRight3.setText(item.getTimelinessCode_s());
            tv_content3.setText("发布部门："+StringUtils.toStringForWu(item.getPubInputOrg_s()));

            linearView0.setVisibility(View.VISIBLE);
            labelRight3.setVisibility(StringUtils.isEmpty(item.getTimelinessCode_s())?View.GONE:View.VISIBLE);
            tv_content3.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("3")){
            //罪名库
            tv_content3.setText(item.getPowerNo_s());
            labelRight3.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("4")){
            //科技文献库
            labelRight1.setText(item.getResourceType_s());
            tv_content1.setText("发布时间："+TimeUtils.getTime(item.getPublishTime_date(),"yyyy-MM-dd"));
            tv_content2.setText("作者："+StringUtils.toStringForWu(item.getAuthor_s()));

            linearView0.setVisibility(View.VISIBLE);
            linearView1.setVisibility(View.VISIBLE);
            labelRight1.setVisibility(StringUtils.isEmpty(item.getResourceType_s())?View.GONE:View.VISIBLE);
            tv_content1.setVisibility(View.VISIBLE);
            tv_content2.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("5")){
            //专家库
            tv_content3.setText("工作单位："+StringUtils.toStringForWu(item.getWorkUnit_s()));
            tv_content4.setText("研究方向："+StringUtils.toStringForWu(item.getResearchDirection_s()));
            labelRight2.setText(item.getClueDomainName_s());
            linearView0.setVisibility(View.VISIBLE);
            labelRight2.setVisibility(StringUtils.isEmpty(item.getResearchDirection_s())?View.GONE:View.VISIBLE);
            tv_content3.setVisibility(View.VISIBLE);
            tv_content4.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("6")){
            //鉴定机构库
            tv_content3.setText("联系方法："+StringUtils.toStringForWu(item.getContactWay_s()));
            tv_content4.setText("地址："+StringUtils.toStringForWu(item.getAddress_s()));
            labelRight2.setText(item.getOrgType_s());
            linearView0.setVisibility(View.VISIBLE);
            labelRight2.setVisibility(StringUtils.isEmpty(item.getOrgType_s())?View.GONE:View.VISIBLE);
            tv_content3.setVisibility(View.VISIBLE);
            tv_content4.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("8")){
            //专业术语
            tv_content3.setText(item.getPowerNo_s());
            tv_content3.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("14")){
            //权力清单
            labelLeft.setText(item.getPowerType_s());
            labelRight1.setText(item.getIndustryType_s());
            tv_content3.setText("事项编码："+StringUtils.toStringForWu(item.getPowerNo_s()));

            linearView0.setVisibility(View.VISIBLE);
            labelLeft.setVisibility(StringUtils.isEmpty(item.getPowerType_s())?View.GONE:View.VISIBLE);
            labelRight1.setVisibility(StringUtils.isEmpty(item.getIndustryType_s())?View.GONE:View.VISIBLE);
            tv_content3.setVisibility(View.VISIBLE);
        }else if(item.getDataType_i().equals("13")){
            //执法主体
            labelRight1.setText(item.getIndustryType_s());
            tv_content1.setText("区域："+StringUtils.toStringForWu(item.getDictionName_s()));
            tv_content2.setText("领导："+StringUtils.toStringForWu(item.getSubjectLeader_s()));

            linearView0.setVisibility(View.VISIBLE);
            labelRight1.setVisibility(StringUtils.isEmpty(item.getIndustryType_s())?View.GONE:View.VISIBLE);
            linearView1.setVisibility(View.VISIBLE);
            tv_content1.setVisibility(View.VISIBLE);
            tv_content2.setVisibility(View.VISIBLE);
        }
    }

    private void initWebView(WebView webView,String title,String leftSpan,String rightSpan1, String rightSpan2) {
        WebSettings wvSetting = webView.getSettings();
        wvSetting.setJavaScriptEnabled(true);
        wvSetting.setAllowFileAccess(true);
        wvSetting.setBuiltInZoomControls(false);
        wvSetting.setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view,String url) {
                webView.loadUrl("javascript:insertData('"+title+"','"+leftSpan+"','"+rightSpan1+"','"+rightSpan2+"')");
            }
        });
        webView.setWebChromeClient(new WebChromeClient());
    }
}
